package com.cloudsigns.factorymanager2.engineering.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.LoginActivity;
import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.AccountInfoBean;
import com.cloudsigns.factorymanager2.engineering.fragment.BaseFragmentActivity;
import com.cloudsigns.factorymanager2.engineering.fragment.EngineeringOrderManagementForManageFragment;
import com.cloudsigns.factorymanager2.engineering.fragment.EngineeringOrderManagementForSuperFragment;
import com.cloudsigns.factorymanager2.engineering.fragment.MapForSuperFragment;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.DropDownPoupWindow;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;
import com.cloudsigns.factorymanager2.project.fragment.InternalOfficeFragment;
import com.cloudsigns.factorymanager2.project.fragment.SettingFragmet;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;

/**
 * Created by tony on 2018/5/3.
 */

public class EngineeringMainForSuperActivity extends BaseFragmentActivity implements View.OnClickListener {
    private View ll_op_one, ll_op_two;
    private ImageView img_op_one, img_op_two;
    private RelativeLayout rl_back;
    private View rl_head;
    private ImageView img_head;
    private TextView tv_headtop, tv_headbottom;
    private TextView tv_title;
    private CustomViewPager viewpager;
    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;
    private RelativeLayout layout_panel, layout_management, layout_data, layout_system;
    private Fragment workSheetListFragment, internalOffficeFragemnt, systemFragment;
    private MapForSuperFragment mapForSuperFragment;
    private PageIndicatorView indicator;
    private DropDownPoupWindow popupWindow;
    public static final int PANEML = 0;
    public static final int MNAGEMENT = 1;
    public static final int DATA = 2;
    public static final int SYSTEM = 3;
    public int currentIndex = PANEML;

    public static final int INTELLIGENT = 0;
    public static final int MAINTENANCE = 1;
    public static final int ENGINEERING = 2;
    public int currentStatuIndex = INTELLIGENT;
    private String tag = "EngineeringMainForSuperActivity";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_engineeringmain_for_super);

        init();
        viewpager.setCurrentItem(PANEML);
        tv_title.setText(getResources().getText(R.string.work_panel));


    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            boolean isExit = intent.getBooleanExtra(ConstantUtil.KEY_1, false);
            if (isExit) {
                Intent mItent = new Intent(this, LoginActivity.class);
                startActivity(mItent);
                this.finish();
            }
        }
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        ll_op_one = findViewById(R.id.ll_op_one);
        ll_op_two = findViewById(R.id.ll_op_two);
        img_op_one = findViewById(R.id.img_op_one);
        img_op_two = findViewById(R.id.img_op_two);
        tv_title = findViewById(R.id.tv_title);
        rl_head = findViewById(R.id.rl_head);
        tv_headtop = findViewById(R.id.tv_headtop);
        tv_headbottom = findViewById(R.id.tv_headbottom);

        layout_panel = findViewById(R.id.layout_panel);
        layout_management = findViewById(R.id.layout_management);
        layout_data = findViewById(R.id.layout_data);
        layout_system = findViewById(R.id.layout_system);
        viewpager = (CustomViewPager) findViewById(R.id.viewpager);
        viewpager.setScanScroll(false);
        indicator = findViewById(R.id.indicator);

        rl_head.setVisibility(View.VISIBLE);


        rl_back.setVisibility(View.GONE);
        ll_op_one.setOnClickListener(this);
        ll_op_two.setOnClickListener(this);
        layout_panel.setOnClickListener(this);
        layout_management.setOnClickListener(this);
        layout_data.setOnClickListener(this);
        layout_system.setOnClickListener(this);

        mapForSuperFragment = new MapForSuperFragment();
        internalOffficeFragemnt = new InternalOfficeFragment();
        systemFragment = new SettingFragmet();


        listFragments = new ArrayList<Fragment>();
        listFragments.add(mapForSuperFragment);

        workSheetListFragment = new EngineeringOrderManagementForSuperFragment();
        listFragments.add(workSheetListFragment);

        AccountInfoBean accountInfoBean = MyApplicaton.getApplication().getAccountInfoBean();
        if (null != accountInfoBean) {
            if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.SUPER) {
                tv_headbottom.setText("项目部/" + accountInfoBean.getAccountName());
                String companyName = accountInfoBean.getCompanyName();
                if (!TextUtils.isEmpty(companyName) && !"null".equals(companyName)) {
                    tv_headtop.setText(companyName);
                }
            }

        }


        listFragments.add(internalOffficeFragemnt);
        listFragments.add(systemFragment);

        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(4);

        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.rgb_F67B3D));

        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                switchTab(position);
                currentIndex = position;

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

        img_op_one.setBackgroundResource(R.mipmap.dituqiehuan);
        img_op_two.setBackgroundResource(R.mipmap.sebiao);
        ll_op_one.setVisibility(View.VISIBLE);
        ll_op_two.setVisibility(View.VISIBLE);

    }

    private void switchTab(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            case PANEML:
                layout_panel.setSelected(true);
                layout_panel.setBackgroundColor(getResources().getColor(R.color.rgb_F67B3D));

                layout_management.setSelected(false);
                layout_management.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));

                layout_data.setSelected(false);
                layout_data.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));

                layout_system.setSelected(false);
                layout_system.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));

//                title_layout.setVisibility(View.GONE);
//
                tv_title.setText(getResources().getText(R.string.work_panel));
//
                img_op_one.setBackgroundResource(R.mipmap.title_icon_menu);
                img_op_two.setBackgroundResource(R.mipmap.icon_hand);
                ll_op_one.setVisibility(View.VISIBLE);
                ll_op_two.setVisibility(View.VISIBLE);

                rl_head.setVisibility(View.VISIBLE);
                rl_back.setVisibility(View.GONE);

                break;
            case MNAGEMENT:
                layout_panel.setSelected(false);
                layout_panel.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));

                layout_management.setSelected(true);
                layout_management.setBackgroundColor(getResources().getColor(R.color.rgb_F67B3D));

                layout_data.setSelected(false);
                layout_data.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));

                layout_system.setSelected(false);
                layout_system.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));

//                title_layout.setVisibility(View.VISIBLE);
                tv_title.setText(getResources().getText(R.string.work_order_management));


                ll_op_one.setVisibility(View.GONE);
                ll_op_two.setVisibility(View.GONE);

                rl_head.setVisibility(View.GONE);
                rl_back.setVisibility(View.GONE);
                break;
            case DATA:
                layout_panel.setSelected(false);
                layout_panel.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));

                layout_management.setSelected(false);
                layout_management.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));

                layout_data.setSelected(true);
                layout_data.setBackgroundColor(getResources().getColor(R.color.rgb_F67B3D));

                layout_system.setSelected(false);
                layout_system.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));

//                title_layout.setVisibility(View.VISIBLE);
                tv_title.setText(getResources().getText(R.string.inner_office));

                ll_op_one.setVisibility(View.GONE);
                ll_op_two.setVisibility(View.GONE);

                rl_head.setVisibility(View.GONE);
                rl_back.setVisibility(View.GONE);
                break;
            case SYSTEM:
                layout_panel.setSelected(false);
                layout_panel.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));

                layout_management.setSelected(false);
                layout_management.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));

                layout_data.setSelected(false);
                layout_data.setBackgroundColor(getResources().getColor(R.color.rgb_f2f2f2));

                layout_system.setSelected(true);
                layout_system.setBackgroundColor(getResources().getColor(R.color.rgb_F67B3D));

//                title_layout.setVisibility(View.VISIBLE);
                tv_title.setText(getResources().getText(R.string.system));

                ll_op_one.setVisibility(View.GONE);
                ll_op_two.setVisibility(View.GONE);

                rl_head.setVisibility(View.GONE);
                rl_back.setVisibility(View.GONE);
                break;

        }

    }

    private void showPopupWindow(View anchor) {
        if (null != popupWindow)
            popupWindow.dismiss();

        popupWindow = new DropDownPoupWindow(EngineeringMainForSuperActivity.this, tag, 0, R.layout.popwindow_map_for_super_engineering, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOnClickListener(this);
        popupWindow.setCusOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {


            }
        });
        popupWindow.showPopupWindow(anchor, anchor.getLayoutParams().width / 2, 5);
    }

    private void showStatusPopupWindwo(View anchor) {
        if (null != popupWindow)
            popupWindow.dismiss();

        switch (currentStatuIndex) {

            case INTELLIGENT:
                break;
            case MAINTENANCE:
                popupWindow = new DropDownPoupWindow(EngineeringMainForSuperActivity.this, "", -1, R.layout.popwindow_map_for_super_maintenance_status, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setOnClickListener(this);
                popupWindow.setCusOnDismissListener(new PopupWindow.OnDismissListener() {

                    @Override
                    public void onDismiss() {


                    }
                });
                popupWindow.showPopupWindow(anchor, anchor.getLayoutParams().width / 2, 5);
                break;
            case ENGINEERING:
                popupWindow = new DropDownPoupWindow(EngineeringMainForSuperActivity.this, "", -1, R.layout.popwindow_map_for_super_engineering_status, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setOnClickListener(this);
                popupWindow.setCusOnDismissListener(new PopupWindow.OnDismissListener() {

                    @Override
                    public void onDismiss() {


                    }
                });
                popupWindow.showPopupWindow(anchor, anchor.getLayoutParams().width / 2, 5);
                break;
        }

    }


    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.ll_op_one:
                showPopupWindow(view);
                break;
            case R.id.ll_op_two:
                showStatusPopupWindwo(view);
                break;
            case R.id.rl_intelligent_control:
                currentStatuIndex = INTELLIGENT;
                showStatusPopupWindwo(ll_op_two);
                mapForSuperFragment.getOrderList(currentStatuIndex);
                break;
            case R.id.rl_maintenance_operation:
                currentStatuIndex = MAINTENANCE;
                showStatusPopupWindwo(ll_op_two);
                mapForSuperFragment.getOrderList(currentStatuIndex);
                break;
            case R.id.rl_engineering_management:
                currentStatuIndex = ENGINEERING;
                showStatusPopupWindwo(ll_op_two);

                mapForSuperFragment.getOrderList(currentStatuIndex);

                break;

            case R.id.layout_panel:
                if (currentIndex != PANEML) {

                    currentIndex = PANEML;
                    viewpager.setCurrentItem(PANEML);
                    switchTab(PANEML);
                }

                break;
            case R.id.layout_management:
                if (currentIndex != MNAGEMENT) {
                    currentIndex = MNAGEMENT;

                    viewpager.setCurrentItem(MNAGEMENT);
                    switchTab(MNAGEMENT);
                }

                break;
            case R.id.layout_data:
                if (currentIndex != DATA) {
                    currentIndex = DATA;

                    viewpager.setCurrentItem(DATA);
                    switchTab(DATA);
                }

                break;
            case R.id.layout_system:
                if (currentIndex != SYSTEM) {
                    currentIndex = SYSTEM;

                    viewpager.setCurrentItem(SYSTEM);
                    switchTab(SYSTEM);
                }
                break;


        }

    }


}
